package com.xxzb.pack0819;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;

//字节的输入流操作
public class DemoB {
    public static void a() throws Exception {
        //代表a.txt文件
        File file = new File("E:\\yongpangaofeng\\a.txt");
        //建立a.txt和java之间的流通道
        InputStream in = new FileInputStream(file);
        //长度为18的字节数组
        byte[] bytes = new byte[20];
        //将输入过来的数据存放到数组中，len代表的是实际读取长度
        int len = in.read(bytes);
        System.out.println("实际存放了:" + len);
        System.out.println(Arrays.toString(bytes));
        String str = new String(bytes, 0, len);
        System.out.println(str);
        //关闭流通道
        in.close();
    }

    //循环的读取,每次读取10个
    public static void b() throws Exception {
        File file = new File("E:\\yongpangaofeng\\a.txt");
        InputStream in = new FileInputStream(file);
        byte[] bytes = new byte[10];
        String str = "";
        //实际读取的内容，如果没有内容了len的值是-1
        int len;
        while ((len = in.read(bytes)) != -1) {
            System.out.println(Arrays.toString(bytes));
            str += new String(bytes, 0, len);
        }
        System.out.println(str);
        in.close();
    }

    public static void main(String[] args) throws Exception {
//        a();
        b();
    }
}
